package com.mooc.lib_video.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.mooc.lib_video.R

//自定义view绘制流程:onMeasure()测量<不会只执行一次>;onLayout()布置子view位置<不会只执行一次>;onDraw()绘制
class AnchorBehavior(): CoordinatorLayout.Behavior<View>(){

    lateinit var context: Context
    var anchorId = 0
    constructor(ctx: Context,id : Int):this(){
        context = ctx
        anchorId = id
    }
    //重载2参数构造器->获取自定义属性值
    constructor(ctx: Context,sets: AttributeSet) : this() {
        context = ctx
        //自定义属性解析
        val array = context.obtainStyledAttributes(sets,
            R.styleable.anchor_behavior,0,0)
        anchorId = array.getResourceId(R.styleable.
        anchor_behavior_anchorId,0)
        array.recycle()
//        Log.e("ZXY","AnchorBehavior:"+(anchorId == R.id.detail_player_view))
//        Log.e("ZXY","AnchorBehavior:"+(anchorId == R.id.detail_author))
//        Log.e("ZXY","AnchorBehavior:"+(anchorId == R.id.detail_values))
//        Log.e("ZXY","AnchorBehavior:"+(anchorId == R.id.detail_tag))
    }

    //CoordinatorLayout->测量直接子view宽高
    //parent:父容器
    //child:要测量的子控件
    //parentWidthMeasureSpec:宽度的测量模式
    //widthUsed:要使用的宽
    //return:true CoordinatorLayout 不会在测量child 会使用我们给定的高度值
    override fun onMeasureChild(
        parent: CoordinatorLayout,
        child: View,
        parentWidthMeasureSpec: Int,
        widthUsed: Int,
        parentHeightMeasureSpec: Int,
        heightUsed: Int
    ): Boolean {
        //获取使用anchorbehavior控件位置
        //通过锚点id获取到锚点view对象
        //获取到锚点view的位置
        //当前view的位置向下排列->排列当前view的位置到锚点view的下面
        val anchorView = parent.findViewById<View>(anchorId) ?: return false
        //防止子布局中具有margin属性->通知父容器使用的位置:父容器已经用过的位置+margintop
        val lp : CoordinatorLayout.LayoutParams = child.layoutParams as CoordinatorLayout.LayoutParams
        val top = lp.topMargin
        //bottom->getbottom
        val height = top + anchorView.bottom
        //告诉父容器使用的高度->父容器测量NestedScrollView时从这个Y(高度向下)
        parent.onMeasureChild(child,parentWidthMeasureSpec,widthUsed,parentHeightMeasureSpec,height)
        return true
    }

    //CoordinatorLayout->布置子view的位置
    override fun onLayoutChild(
        parent: CoordinatorLayout,
        child: View,
        layoutDirection: Int
    ): Boolean {
        val anchorView = parent.findViewById<View>(anchorId) ?: return false
        val lp : CoordinatorLayout.LayoutParams = child.layoutParams as CoordinatorLayout.LayoutParams
        val top = lp.topMargin
        val bottom = anchorView.bottom
        parent.onLayoutChild(child, layoutDirection)
        //当前的nestedscroll 向下移动锚点view的底部Y值+外边距
//        Log.e("ZXY","onLayoutChild:"+(bottom+top))
        child.offsetTopAndBottom(bottom+top)
        return true
    }

    //给应用当前behavior的子view指定依赖布局
    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: View,
        dependency: View
    ): Boolean {
        return dependency.id == anchorId
    }

}